<template>
  <div id="main-div">
    <el-card style="width: 500px;height: 300px;margin: 50px auto;background-color: rgba(255,255,255,0)">
      <el-form label-width="80px" style="margin-top: 50px;width: 430px">
        <el-form-item label="用户名">
          <el-input type="text" v-model="user.username">请输入用户名</el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="text"  v-model="user.password">请输入密码</el-input>
        </el-form-item>
        <el-form-item  style="position: relative;left: 120px">
          <el-button type="primary" @click="login()">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import {ref} from "vue";
import qs from 'qs'
import axios from "axios";
import router from "@/router";
const user=ref({username:"",password:""})

const login=()=>{
  let data=qs.stringify(user.value)
  axios.post('http://localhost:8080/v1/users/login',data)
    .then((response)=>{
      if (response.data.code==1){
        alert("登录成功")
        //router.push('/')//登录成功返回首页
        location.href="/";//让浏览器请求工程主地址,整个页面都会刷新
        //把登录成功时的用户信息保存到LocalStorage里面
        localStorage.user=JSON.stringify(response.data.data);
      }else {
        alert(response.data.msg)
      }
    })
}
</script>

<style scoped>
#main-div{
  overflow: hidden;
  height: 500px;
  background-image: url('/public/imgs/cxk.gif');
  background-size: cover;/*设置背景图片为封面*/
  background-position: center;/*设置背景图片居中*/
}
</style>